package com.codrim.cs.data.server.dao.entity;

import com.codrim.common.utils.bean.BeanUtils;
import com.codrim.common.utils.math.NumberUtils;
import com.codrim.cs.data.rpc.dto.param.StatisticDataDTO;
import com.codrim.cs.data.rpc.dto.result.WholeDayDataResultDTO;
import com.codrim.cs.data.server.dao.entity.unique.key.StatCsAdvAllUniqueKey;
import com.codrim.cs.data.server.dao.realtime.RealTimeData;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @author Liang.Zhuge on 14/07/2017.
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class StatCsAdvAllDO extends AbstractStatCsAllDO<StatCsAdvAllUniqueKey> {
    private Integer advertiserId;
    private Long gc;
    private Float aprice;
    private Long caps;
    private Long sc;
    /**
     * 自己公司为广告主设置的商务经理ID
     */
    private Integer advAdvertiserManagerId;
    /**
     * 自己公司为渠道设置的渠道经理ID
     */
    private Integer advAffiliateManagerId;
    private Integer advAdvertiserManagerDepartmentId;
    private Integer advAffiliateManagerDepartmentId;

    public StatCsAdvAllDO(int sdate, StatisticDataDTO dto) {
        setAdvertiserId(dto.getAdvId());
        setSdate(sdate);
        setSymbol(dto.getCurrencySymbol());
        setAdvAdvertiserManagerId(dto.getAdvAdvertiserManagerId());
        setAdvAffiliateManagerId(dto.getAdvAffiliateManagerId());
        setAdvAdvertiserManagerDepartmentId(dto.getAdvAdvertiserManagerDepartmentId());
        setAdvAffiliateManagerDepartmentId(dto.getAdvAffiliateManagerDepartmentId());
    }

    @Override
    public WholeDayDataResultDTO toWholeDayDataResultDTO() {
        WholeDayDataResultDTO dto = BeanUtils.copyProperties(this, WholeDayDataResultDTO.class);
        dto.setCompanyId(advertiserId);
        return dto;
    }

    public void plus(RealTimeData realTimeData) {
        setClick(add(this.getClick(), realTimeData.getClicks()));
        setUclick(add(getUclick(), realTimeData.getUClicks()));

        Integer rScrubConversions = realTimeData.getScrubConversions();
        Integer rGrossConversions = realTimeData.getGrossConversions();

        final int realConversions = NumberUtils.toInt(rGrossConversions) - NumberUtils.toInt(rScrubConversions);
        setConversion(add(getConversion(), realConversions));
        sc = add(sc, rScrubConversions);
        gc = add(gc, rGrossConversions);
    }

    @Override
    public void plus(AbstractStatCsEntity source) {
        super.plus(source);
        StatCsAdvAllDO cSource = (StatCsAdvAllDO) source;
        this.gc = add(this.gc, cSource.gc);
        this.aprice = add(this.aprice, cSource.aprice);
        this.caps = add(this.caps, cSource.caps);
        this.sc = add(this.sc, cSource.sc);
    }

    @Override
    public void plus(StatisticDataDTO data) {
        super.plus(data);
        this.gc = add(this.gc, data.getGc());
        this.aprice = add(this.aprice, data.getAccessPrice());
        this.caps = add(this.caps, data.getCaps());
        this.sc = add(this.sc, data.getSc());
    }

    @Override
    public StatCsAdvAllUniqueKey createUniqueKey() {
        return new StatCsAdvAllUniqueKey(this.getSdate(), this.getSymbol(),
                this.advertiserId, advAdvertiserManagerId, advAffiliateManagerId);
    }

}
